﻿// -----------------------------------------------------------------------
// <copyright company="Christoph van der Fecht - VDsoft">
// Copyright (C) Christoph van der Fecht - VDsoft - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited,
// unless it is communicated and authorized by the copyright owner.
// </copyright>
// -----------------------------------------------------------------------

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using SeriesManager.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SeriesManager.ViewModel
{
	/// <summary>
	/// View model to view the actors of a series.
	/// </summary>
	public class ActorsViewModel : ViewModelBase
	{
		/// <summary>
		/// The Actors.
		/// </summary>
		private ObservableCollection<Actor> actors = new ObservableCollection<Actor>();

		/// <summary>
		/// The currently selected mirror.
		/// </summary>
		private TVDB.Model.Mirror currentMirror = null;

		/// <summary>
		/// Initializes a new instance of the <see cref="ActorsViewModel"/> class.
		/// </summary>
		public ActorsViewModel()
		{
			if (this.IsInDesignMode)
			{
				Actor actor0 = new Actor
				{
					ImagePath = "http://www.thetvdb.com/banners/actors/79417.jpg",
					Name = "Stana Katic",
					Role = "Detective Kate Beckett",
					SortOrder = 0
				};

				Actor actor1 = new Actor
				{
					ImagePath = "http://www.thetvdb.com/banners/actors/79415.jpg",
					Name = "Nathan Fillion",
					Role = "Richard Castle",
					SortOrder = 0
				};

				Actor actor2 = new Actor
				{
					ImagePath = "http://www.thetvdb.com/banners/actors/79418.jpg",
					Name = "Susan Sullivan",
					Role = "Martha Rodgers",
					SortOrder = 1
				};

				Actor actor3 = new Actor
				{
					ImagePath = "http://www.thetvdb.com/banners/actors/79413.jpg",
					Name = "Molly C. Quinn",
					Role = "Alexis Castle",
					SortOrder = 1
				};

				Actor actor4 = new Actor
				{
					ImagePath = "http://www.thetvdb.com/banners/actors/82269.jpg",
					Name = "Seamus Dever",
					Role = "Kevin Ryan",
					SortOrder = 2
				};

				List<Actor> dummy = new List<Actor>()
				{
					actor0,
					actor1,
					actor2,
					actor3,
					actor4
				};

				this.Actors = new ObservableCollection<Actor>(dummy.OrderBy(x => x.SortOrder).ToList());
			}
			else
			{
				Messenger.Default.Register<TVDB.Model.Mirror>(this, ViewModelLocator.SearchSeriesToken, this.ReceiveMirror);
				Messenger.Default.Register<List<Model.Actor>>(this, ViewModelLocator.SeriesActorToken, this.ReceiveActors);
			}
		}

		/// <summary>
		/// Gets or sets the Actors.
		/// </summary>
		public ObservableCollection<Actor> Actors
		{
			get
			{
				return this.actors;
			}

			set
			{
				if (value == this.actors)
				{
					return;
				}

				this.actors = value;
				this.RaisePropertyChanged();
			}
		}

		/// <summary>
		/// Receives all actors of the series.
		/// </summary>
		/// <param name="actors">Collection of <see cref="Actors"/>.</param>
		private void ReceiveActors(List<Model.Actor> actors)
		{
			if (actors == null || actors.Count == 0)
			{
				return;
			}

			foreach (Actor current in actors)
			{
				current.ImagePath = string.Format("{0}/banners/{1}", this.currentMirror.Address, current.ImagePath);
			}

			this.Actors = new ObservableCollection<Actor>(actors);
		}

		/// <summary>
		/// Receives the selected mirror.
		/// </summary>
		/// <param name="received">Mirror which was selected by the user.</param>
		private void ReceiveMirror(TVDB.Model.Mirror received)
		{
			if (received == null)
			{
				return;
			}

			this.currentMirror = received;
		}
	}
}
